using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using ServiceMReviewer.Model;

namespace ServiceMReviewer.DTO
{
	/// <summary>
	/// Data transfer object which is associated with Review table in database
	/// </summary>
	[DataContract]
	public class ReviewDTO
	{
		[DataMember]
		public int ID { get; set; }
        [DataMember]
        public string IDEstablishment { get; set; }
        [DataMember]
        public int IDMealType { get; set; }
		[DataMember]
		public DateTime ReviewDate { get; set; }
		[DataMember]
		public double OverallRating { get; set; }
		[DataMember]
		public double MealCost { get; set; }
		[DataMember]
		public double ServiceRating { get; set; }
		[DataMember]
		public double AtmosphereRating { get; set; }
		[DataMember]
		public double FoodRating { get; set; }
		[DataMember]
		public string TitleComment { get; set; }
		[DataMember]
		public string ContentComment { get; set; }

        [DataMember]
        public EstablishmentDTO Establishment { get; set; }
        [DataMember]
        public MealTypeDTO MealType { get; set; }

        public static ReviewDTO Translate(Review Entity)
		{
			var dto = new ReviewDTO
			{
                ID = Entity.ID,
				ReviewDate = Entity.ReviewDate == null ? DateTime.MinValue : Entity.ReviewDate.Value,
                OverallRating = Entity.OverallRating == null ? 0 : Entity.OverallRating.Value,
                MealCost = Entity.MealCost == null ? 0 : Entity.MealCost.Value,
                ServiceRating = Entity.ServiceRating == null ? 0 : Entity.ServiceRating.Value,
                AtmosphereRating = Entity.AtmosphereRating == null ? 0 : Entity.AtmosphereRating.Value,
                FoodRating = Entity.FoodRating == null ? 0 : Entity.FoodRating.Value,
                TitleComment = Entity.TitleComment,
				ContentComment = Entity.ContentComment
			};

            if (Entity.Establishment != null)
            {
                dto.IDEstablishment = Entity.IDEstablishment;
                dto.Establishment = EstablishmentDTO.TranslateBase(Entity.Establishment);
            }

            if (Entity.MealType != null)
            {
                dto.IDMealType = Entity.IDMealType.Value;
                dto.MealType = MealTypeDTO.Translate(Entity.MealType);
            }

			return dto;
		}

        public static Review Reverse(ReviewDTO obj)
        {
            var entity = new Review
            {
                IDEstablishment = obj.IDEstablishment,
                IDMealType = obj.IDMealType,
                ReviewDate = obj.ReviewDate,
                OverallRating = obj.OverallRating,
                MealCost = obj.MealCost,
                ServiceRating = obj.ServiceRating,
                AtmosphereRating = obj.AtmosphereRating,
                FoodRating = obj.FoodRating,
                TitleComment = obj.TitleComment,
                ContentComment = obj.ContentComment
            };
            return entity;
        }

        public void UpdateEntity(ref Review entity)
        {
            entity.IDEstablishment = this.IDEstablishment;
            entity.IDMealType = this.IDMealType;
            entity.ReviewDate = this.ReviewDate;
            entity.OverallRating = this.OverallRating;
            entity.MealCost = this.MealCost;
            entity.ServiceRating = this.ServiceRating;
            entity.AtmosphereRating = this.AtmosphereRating;
            entity.FoodRating = this.FoodRating;
            entity.TitleComment = this.TitleComment;
            entity.ContentComment = this.ContentComment;
        }
	}
}